<?php
namespace app\admin\controller;
use think\Request;
use think\Db;
use think\Config;

class Suppliercashapply extends Base
{
    /**
     * 供应商提现申请
     * @return [type] [description]
     */
    function index(){
        $keywords = Request::instance()->param("keywords",'');
        $apply_status = Request::instance()->param("apply_status",'','intval');
        $query= Db::name('supplier_cash_apply')->alias('SCA')
            ->field('SCA.*,S.supplier_name,SU.username')
            ->join('supplier S','SCA.supplier_id = S.supplier_id','LEFT')
            ->join('supplier_user SU','SCA.supplier_user_id = SU.id','LEFT');

        if ( !empty($keywords) ) {
            $query->where('S.supplier_name|SU.username', 'like', '%'.$keywords.'%');
        }


        if (!empty($apply_status)){
            $query->where('apply_status',$apply_status);
        }
        $data['list'] = $query->paginate('',false,['query'=>Request()->param()]);
        return $this->fetch('index',$data);
    }


    /**
     * 审核详情
     * @return [type] [description]
     */
    function info(){

        if (Request()->isPost()) { //审核处理
            $params = Request()->only('apply_id,apply_status,refuse_reason');


            if ($params['apply_status'] == -1 && empty($params['refuse_reason'])) {
                $this->error('请填写拒绝理由！');
            }

            Db::startTrans();

            $res = Db::name('supplier_cash_apply')->where(['apply_id'=>$params['apply_id']])->setField('apply_status',$params['apply_status']);//更新状态

            $apply_info = Db::name('supplier_cash_apply')->where(['apply_id'=>$params['apply_id']])->find();

            if ($params['apply_status'] == -1) { //更新拒绝理由并且发送短信提醒拒绝原因
                $res_2 = Db::name('supplier_cash_apply')->where(['apply_id'=>$params['apply_id']])->setField('refuse_reason',$params['refuse_reason']);
                $res_3 = Db::name('supplier_cash_apply')->where(['apply_id'=>$params['apply_id']])->setField('admin_id',$this->uid);

            }elseif($params['apply_status'] == 2){//审核通过 余额减少  供应商账目明细 insert

                $res_2 = true;
                $res_3 = Db::name('supplier_cash_apply')->where(['apply_id'=>$params['apply_id']])->setField('admin_id',$this->uid);

            }

            if ($res!==false && $res_2!==false && $res_3!==false) {
                Db::commit();
                $this->success('操作成功','','refresh');
            }else{
                Db::rollback();
                $this->error('操作失败');
            }

        }else{
            $params = Request()->param();
            !isset($params['apply_id']) && $this->error('参数错误');

            $info = Db::name('supplier_cash_apply')->alias('SCA')
                ->field('SCA.*,S.supplier_name,S.balance,S.charge,S.charge_phone,SU.username')
                ->join('supplier S','SCA.supplier_id = S.supplier_id','LEFT')
                ->join('supplier_user SU','SCA.supplier_user_id = SU.id','LEFT')
                ->where(['apply_id'=>$params['apply_id']])
                ->find();

            if (!empty($info['admin_id'])){
                $admin_info = Db::name('admins')->where(['id'=>$info['admin_id']])->find();
                $info['admin_text'] = '['.$admin_info['username'].']'.$admin_info['name'].'('.$admin_info['phone'].')';
            }else{
                $info['admin_text'] = '暂无审核人';
            }

            $data = ['info'=>$info];
            return $this->fetch('info',$data);
        }
    }


    /**
     * 发送模板消息
     * @param  [type] $info [description]
     * @return [type]        [description]
     */
    public function sendTmp($info,$password){
        import('Alidayu.TopSdk');
        import('Alidayu.top.TopClient');
        import('Alidayu.top.request.AlibabaAliqinFcSmsNumSendRequest');
        $appkey="23535194";
        $secret="48953173553ad6ad6b306138f518d7f1";

        $c = new \TopClient();
        $c ->appkey = $appkey ;
        $c ->secretKey = $secret ;
        $req = new \AlibabaAliqinFcSmsNumSendRequest();
        $req ->setExtend( "" );
        $req ->setSmsType( "normal" );
        $req ->setSmsFreeSignName( "速冻在线" );
        $req ->setSmsParam("{name:'".$info['supplier_name']."',account:'".$info['username']."',password:'".$password."'}");
        $req ->setRecNum( $info['charge_phone'] );
        $req ->setSmsTemplateCode( "SMS_116480065" );
        $re = $c ->execute( $req );
    }
}